<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="showAdd(1, '0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button :disabled="row.type === 2" size="small" type="text" @click="showAdd(2, row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="showEdit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 新增编辑的弹层 -->
      <el-dialog :visible.sync="showDialog" width="40%" :title="formData.id? '编辑权限弹窗': '添加权限弹窗'" @close="close">
        <!-- 表单内容 -->
        <el-form ref="form" label-width="100px" :model="formData" :rules="rules">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="启用"
              active-value="1"
              inactive-value="0"
              inactive-text="禁用"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: center;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="add">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, addPermission, getPermissionDetail, updatePermission, delPermission } from '@/api/permisson'
import { listToTree } from '@/utils'
export default {
  name: 'Permission',

  data() {
    return {
      // 存放所有权限（树形）
      list: [],
      // 添加权限&添加权限点，对话框
      showDialog: false,
      // 表单数据
      formData: {
        enVisible: '1', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: ['change', 'blur'] }],
        code: [{ required: true, message: '权限标识不能为空', trigger: ['change', 'blur'] }]
      }
    }
  },

  created() {
    // 获取所有权限
    this.getPermissionList()
  },

  methods: {
    // 获取所有权限
    async getPermissionList() {
      const res = await getPermissionList()
      console.log(res)
      this.list = listToTree(res, '0')
      console.log(this.list)
    },

    // 添加权限
    showAdd(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },

    // 点击确定，添加&编辑权限
    async add() {
      try {
        // 校验表单
        await this.$refs.form.validate()
      } catch (error) {
        return
      }
      if (this.formData.id) {
        // 查看编辑,发送请求
        await updatePermission(this.formData)
        this.$message.success('编辑成功')
      } else {
        // 添加,发送请求
        await addPermission(this.formData)
        this.$message.success('添加成功')
      }

      this.showDialog = false
      // 重新渲染
      this.getPermissionList()
    },

    close() {
      // 重置数据
      this.$refs.form.resetFields()
      this.formData = {
        enVisible: '1', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },

    // 点击查看编辑
    async showEdit(id) {
      this.showDialog = true
      this.formData = await getPermissionDetail(id)
    },

    // 删除
    async del(id) {
      await this.$confirm('此操作将永久删除该文件是否继续?', '提示', {
        type: 'warning'
      })
      await delPermission(id)
      this.getPermissionList()
      this.$message.success('删除成功')
    }
  }
}
</script>
